from sklearn import preprocessing
from sklearn.ensemble import RandomForestClassifier
import numpy as np
# 加载数据集
input_file = "F:/python学习资料/Python-Machine-Learning-Cookbook-master/Chapter02/car.data.txt"
# 读取数据
X = []
count = 0
with open(input_file,'r') as f:
    for line in f.readlines():
        data = line[:-1].split(",")
        X.append(data)

X = np.array(X)

# 将字符串转换成数值
label_encoder = []
X_encoded = np.empty(X.shape)
print(X[0])
for i, item in enumerate(X[0]):
    label_encoder.append(preprocessing.LabelEncoder())
    X_encoded[:,i] = label_encoder[-1].fit_transform(X[:,i])

X = X_encoded[:,:-1].astype(int)
y = X_encoded[:,-1].astype(int)

# 训练分类器
params = {"n_estimators":200,'max_depth':8,'random_state':7}
classifier = RandomForestClassifier(**params)
classifier.fit(X,y)
# 交叉验证
from sklearn import model_selection
accuracy = model_selection.cross_val_score(classifier,X,y,scoring='accuracy',cv=3)
print("Accuracy of the classifier:"+str(round(100*accuracy.mean(),2))+"%")

# 预测数据
input_data = ['high','low','2','more','med','high']
input_data_encoded = [-1] * len(input_data)
for i,item in enumerate(input_data):
    input_data_encoded[i] = int(label_encoder[i].transform([input_data[i]]))

input_data_encoded =np.array(input_data_encoded)

output_class = classifier.predict([input_data_encoded])
print(output_class)
print(label_encoder[-1].inverse_transform(output_class))
print("输出标签:",label_encoder[-1].inverse_transform(output_class)[0])

# 验证曲线
import matplotlib.pyplot as plt
from sklearn.model_selection import validation_curve

classifier = RandomForestClassifier(max_depth=4,random_state=7)

parameter_grid = np.linspace(25,200,8).astype(int)

train_scores,validation_scores = validation_curve(classifier,X,y,'n_estimators',parameter_grid,cv=5)
print("#### VALIDATION CURVES ######")
print("\nParam: n_estimators\nTraining scores:\n",train_scores)
print("\nParam:n_estimators\nValidation scores:\n",validation_scores)

plt.figure()
plt.plot(parameter_grid,100*np.average(train_scores,axis=1),color='black')
plt.title("Training curve")
plt.xlabel("Number of estimators")
plt.ylabel("Accuracy")
plt.show()

classifier = RandomForestClassifier(n_estimators=20,random_state=7)
parameter_grid = np.linspace(2,10,5)
train_scores,validation_scores = validation_curve(classifier,X,y,'max_depth',parameter_grid,cv=5)
print("\nParam:max_depth\nTraining scores:\n",train_scores)
print("\nParam:max_depth\nValidation scores:\n",validation_scores)